package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"housing/models/mysql"

	"housing/houserpc/houserpc"
	"housing/houserpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type PreDelLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPreDelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PreDelLogic {
	return &PreDelLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *PreDelLogic) PreDel(in *houserpc.PreDelRequest) (*houserpc.PreDelResponse, error) {
	// todo: add your logic here and delete this line

	begin := mysql.DB.Begin()

	pre := mysql.Pre{
		Model:  gorm.Model{ID: uint(in.PreId)},
		Status: "预约中",
	}
	err := begin.Debug().Updates(&pre).Error

	if err != nil {
		begin.Rollback()
		return nil, fmt.Errorf("已取消")
	}
	begin.Commit()
	return &houserpc.PreDelResponse{}, nil
}
